package com.education.platform.management.controller.paper;


import com.education.platform.common.enums.DicTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.ExaminPaperDO;
import com.education.platform.dao.entity.QuestionBankDO;
import com.education.platform.dao.entity.RelPaperQuestionDO;
import com.education.platform.dao.entity.SysDictionaryDO;
import com.education.platform.management.service.ExaminPaperService;
import com.education.platform.management.service.RelPaperQuestionService;
import com.education.platform.management.service.SysDictionaryService;
import com.education.platform.management.vo.request.ExaminPaperQueryVO;
import com.education.platform.management.vo.request.ExaminPaperUpdateVO;
import com.education.platform.management.vo.response.ExaminPaperPageInfoVO;
import com.education.platform.management.vo.response.ExaminPaperResponseVO;
import com.education.platform.management.vo.response.QuestionBankResponseVO;
import com.education.platform.management.vo.response.SysDictionaryResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
* describe: 试卷
* @author :
* @date: 2020-07-15 08:43:11.650
**/
@RestController
@Api("试卷")
@RequestMapping("examinPaper")
@Validated
public class ExaminPaperController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExaminPaperController.class);

	@Autowired
    private ExaminPaperService examinPaperService;

    @Autowired
    private SysDictionaryService sysDictionaryService;
    @Autowired
    private RelPaperQuestionService relPaperQuestionService;


    /**
     * 分页查询
     * @param examinPaperQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询试卷")
    @PostMapping("getPageList")
    public ExaminPaperPageInfoVO selectPageList(@RequestBody @Valid ExaminPaperQueryVO examinPaperQueryVO) {

        Page<ExaminPaperResponseVO> page = PageHelper.startPage(examinPaperQueryVO.getPageNum(), examinPaperQueryVO.getPageSize());
        ExaminPaperDO query = BeanUtil.convert(examinPaperQueryVO,ExaminPaperDO.class);
        query.setDeleted(false);
        List<ExaminPaperDO> examinPaperList = examinPaperService.getList(query);
        ExaminPaperPageInfoVO examinPaperPageInfo = new ExaminPaperPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), examinPaperPageInfo);
        examinPaperPageInfo.setPageNum(examinPaperQueryVO.getPageNum());
        List<ExaminPaperResponseVO> voList = BeanUtil.convert(examinPaperList, ExaminPaperResponseVO.class);
        List<Integer> paperIds = examinPaperList.parallelStream().map(ExaminPaperDO::getId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(paperIds)){
            List<RelPaperQuestionDO> rels=relPaperQuestionService.getRelByPaperIds(paperIds);
            if(CollectionUtils.isNotEmpty(rels)){
                Map<Integer,List<RelPaperQuestionDO>> map = rels.parallelStream().collect(Collectors.groupingBy(RelPaperQuestionDO::getPaperId));
                for(ExaminPaperResponseVO responseVO:voList){
                    List<RelPaperQuestionDO>list =  map.get(responseVO.getId());
                    if(CollectionUtils.isEmpty(list)){
                        responseVO.setQuestionIds(Collections.emptyList());
                    }else {
                        responseVO.setQuestionIds(list.parallelStream().map(RelPaperQuestionDO::getQuestionId).collect(Collectors.toList()));
                    }
                }
            }
        }
        examinPaperPageInfo.setList(voList);
        return examinPaperPageInfo;
    }

    @ApiOperation(value = "获取试卷类型")
    @PostMapping("selectPaperType")
    public List<SysDictionaryResponseVO> selectGrade() {
        List<String> typeCodes = Arrays.asList(DicTypeEnum.PAPER_TYPE.getValue());
        List<SysDictionaryDO> dictionaryDTOS = sysDictionaryService.getListByDicTypeCodeList(typeCodes);
        return   BeanUtil.convert(dictionaryDTOS, SysDictionaryResponseVO.class);
    }

    @ApiOperation(value = "新增/修改试卷")
    @PostMapping("save")
    public Boolean save(@RequestBody @Valid ExaminPaperUpdateVO vo) {
         examinPaperService.save( BeanUtil.convert(vo, ExaminPaperDO.class),vo.getQuestionIds());
         return Boolean.TRUE;
    }



    /**
     * 试卷详情
     *
     * @param
     * @return
     */
    @ApiOperation(value = "试卷详情")
    @GetMapping("getTestPaperDetail")
    public List<QuestionBankResponseVO> getTestPaperDetail(@RequestParam Integer paperId) {
        List<QuestionBankDO> questionBankList = examinPaperService.getQuestionsByPaperId(paperId);
        List<QuestionBankResponseVO> voList = new ArrayList<>();
        questionBankList.forEach(m->{
            QuestionBankResponseVO responseVO =  BeanUtil.convert(m, QuestionBankResponseVO.class);
            if (m.getType() == 2) {
                //简单题答案
                responseVO.setShortAnswer(m.getChoiceAnswer());
            } else {
                //选择题答案
                List<Integer> choice=Arrays.asList(m.getChoiceAnswer().split("==")).
                        stream().map(Integer::valueOf).collect(Collectors.toList());
                responseVO.setAnswerIndexList(choice);
                //选项
                responseVO.setOptions(Arrays.asList(m.getOptionA().split("==")));
            }
            voList.add(responseVO);
        });
        return voList;
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public void delete(@RequestBody @Valid List<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<ExaminPaperDO> updateList = new ArrayList<>(ids.size());
        ExaminPaperDO examinPaperDO;
        for(Integer id:ids){
            examinPaperDO=  new ExaminPaperDO();
            examinPaperDO.setId(id);
            examinPaperDO.setDeleted(true);
            updateList.add(examinPaperDO);
        }
        examinPaperService.updateInBatch(updateList);
    }


}
